package com.base;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Builder
@Data
@NoArgsConstructor
public class ResponseResult<T> implements Serializable {
    /**
     * 响应状态码
     */
    private int code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 返回数据
     */
    private T data;

    /**
     * 构造方法
     * @param code
     * @param message
     * @param data
     */
    public ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 响应成功
     * @param msg
     * @param data
     * @return
     */
    public static ResponseResult success(String msg, Object data) {
        return new ResponseResult(Status.SUCCESS_CODE, msg, data);
    }

    /**
     * 响应成功（默认消息）
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(Status.SUCCESS_CODE, "操作成功", data);
    }

    /**
     * 响应失败
     */
    public static ResponseResult error(String msg) {
        return new ResponseResult(Status.ERROR_CODE, msg, null);
    }

    /**
     *  响应失败
     * @param msg
     * @param data
     * @return
     */
    public static ResponseResult error(String msg, Object data) {
        return new ResponseResult(Status.ERROR_CODE, msg, data);
    }

}
